<?php

namespace app\services\data\stat;

use app\clickhouse\models\ModelBase;
use app\services\data\stat\ad\StatBase;
use app\services\data\stat\ad\normal\StatKeep;
use app\services\data\stat\ad\normal\StatRegLtv;
use app\services\data\stat\ad\normal\StatRegLtvFirstDayMoney;
use app\services\data\stat\ad\normal\StatRegLtvMidas;
use app\services\data\stat\ad\normal\StatRegRoi;
use app\services\data\stat\ad\normal\StatRegRoiMidas;
use app\services\data\stat\ad\normal\StatReport;
use app\services\data\stat\ad\normal\StatRoleKeep;
use app\services\ServiceBase;

class AdStatService extends ServiceBase
{
    protected $statDataFields = [
        'firstup' => [
            'class' => 'StatFirstUp',
            'props' => [
                "firstup"    => 0,   // 激活
                "firstup_ip" => 0,   // 激活ip数
            ]
        ],
        'reg' => [
            'class' => 'StatReg',
            'props' => [
                "reg"         => 0,   // 注册
                "reg_ip"      => 0,   // 注册ip数
                "reg_firstup" => 0,   // 注册设备数
            ],
        ],
        'login' => [
            'class' => 'StatLogin',
            'props' => [
                "login" => 0,   // 活跃
            ],
        ],
        'pay' => [
            'class' => 'StatPay',
            'props' => [
                "pay_user"  => 0,   // 充值用户
                "pay_num"   => 0,   // 充值次数
                "pay_money" => 0,   // 充值金额
            ],
        ],
        'regpay' => [
            'class' => 'StatRegPay',
            'props' => [
                "pay_reg"        => 0,   // 注册付费人数
                "pay_reg_num"    => 0,   // 注册付费次数
                "pay_reg_money"  => 0,   // 注册付费金额
                "pay_reg_money2" => 0,   // 注册付费金额(cp分成后)
            ],
        ],
        'role_reg' => [
            'class' => 'StatRoleReg',
            'props' => [
                "role_reg"    => 0,   // 创角
                "role_reg_ip" => 0,   // 创角ip数
            ],
        ],
        'role_reg_new' => [
            'class' => 'StatRoleRegNew',
            'props' => [
                "role_reg_new"    => 0,   // 创角(新)
                "role_reg_ip_new" => 0,   // 创角ip数(新)
            ],
        ],
        'pay_noe' => [
            'class' => 'StatRegPayOne',
            'props' => [
                "pay_one"       => 0,   // 首冲人数
                "pay_one_num"   => 0,   // 首冲次数
                "pay_one_money" => 0,   // 首冲金额
            ],
        ],
        'pay_xuni' => [
            'class' => 'xuni\\StatPay',
            'props' => [
                "pay_user_xuni"  => 0,   // 充值用户-虚拟订单
                "pay_num_xuni"   => 0,   // 充值次数-虚拟订单
                "pay_money_xuni" => 0,   // 充值金额-虚拟订单
            ],
        ],
        'regpay_xuni' => [
            'class' => 'xuni\\StatRegPay',
            'props' => [
                "pay_reg_xuni"        => 0,   // 注册付费人数-虚拟订单
                "pay_reg_num_xuni"    => 0,   // 注册付费次数-虚拟订单
                "pay_reg_money_xuni"  => 0,   // 注册付费金额-虚拟订单
                "pay_reg_money2_xuni" => 0,   // 注册付费金额(cp分成后)-虚拟订单
            ],
        ],
        'toapk_reg' => [
            'class' => 'StatToapkReg',
            'props' => [
                "toapk_reg" => 0,   // 注册并转端用户数
            ],
        ],
        'toapk_reg_all' => [
            'class' => 'StatToapkRegAll',
            'props' => [
                "toapk_reg_all" => 0,   // 累计注册转端用户数
            ],
        ],
        'postback' => [
            'class' => 'StatPostback',
            'props' => [
                "postback_user" => 0, // 回传用户数
                "postback_num" => 0, // 回传次数
                "postback_money" => 0, // 回传金额
            ],
        ],
        'postback_reg' => [
            'class' => 'StatRegPostback',
            'props' => [
                "postback_reg_user" => 0, // 回传用户数(新)
                "postback_reg_num" => 0, // 回传次数(新)
                "postback_reg_money" => 0, // 回传金额(新)
            ],
        ],
        'pay_midas' => [
            'class' => 'StatPayMidas',
            'props' => [
                "midas_pay_user"  => 0,   // 充值用户
                "midas_pay_num"   => 0,   // 充值次数
                "midas_pay_money" => 0,   // 充值金额
            ],
        ],
        'regpay_midas' => [
            'class' => 'StatRegPayMidas',
            'props' => [
                "midas_pay_reg"        => 0,   // 注册付费人数
                "midas_pay_reg_num"    => 0,   // 注册付费次数
                "midas_pay_reg_money"  => 0,   // 注册付费金额
                "midas_pay_reg_money2" => 0,   // 注册付费金额(cp分成后)
            ],
        ],
    ];

    private function formatGroupValue($group_key, $value)
    {
        if ($group_key == 'stat_hour') {
            return intval($value);
        }

        if ($group_key == 'ad_id') {
            return trim($value, "\n");
        }

        return $value;
    }

    public function statData($params, $fields = [])
    {
        $template = [];
        foreach ($fields as $name) {
            $template = array_merge($template, $this->statDataFields[$name]['props']);
        }

        $base = new StatBase($params);
        $groupbys = $base->getGroupbys();

        $list = [];
        $allsum = $template;
        foreach ($fields as $field) {
            if (!isset($this->statDataFields[$field])) {
                continue;
            }
            $class_name = "\\app\\services\\data\\stat\\ad\\normal\\{$this->statDataFields[$field]['class']}";
            $class = new $class_name($params);
            $sql   = $class->getSqlByGroup();
            $stat  = ModelBase::getInstance()->select($sql);
            $data  = $stat->rows();
            foreach ($data as $val) {
                $index = '';
                $tmp   = [];
                foreach ($groupbys as $key) {
                    $v = $this->formatGroupValue($key, $val[$key]);
                    $index .= $v;
                    $tmp[$key] = $v;
                }
                $index = md5($index);

                if (!isset($list[$index])) {
                    $list[$index] = array_merge($tmp, $template);
                }

                foreach ($template as $key => $normal) {
                    $list[$index][$key] += $val[$key] ?? 0;
                    $allsum[$key] += $val[$key] ?? 0;
                }
            }
        }

        $total = $template;
        foreach ($fields as $field) {
            if (!isset($this->statDataFields[$field])) {
                continue;
            }
            $class_name = "\\app\\services\\data\\stat\\ad\\normal\\{$this->statDataFields[$field]['class']}";
            $class = new $class_name($params);
            $sql   = $class->getSql();
            $stat  = ModelBase::getInstance()->select($sql);
            $data  = $stat->rows();
            foreach ($data as $val) {
                foreach ($template as $key => $normal) {
                    $total[$key] += $val[$key] ?? 0;
                }
            }
        }

        return [
            'list'     => $list,
            'total'    => $total,
            'allsum'   => $allsum,
            'groupbys' => $groupbys
        ];
    }

    public function statReport($params)
    {
        $template = [
            "show"  => 0,   // 曝光
            "click" => 0,   // 点击
            "cost1"  => 0,   // 消耗
            "cost2"  => 0,   // 消耗成本
        ];

        $base = new StatBase($params);
        $groupbys = $base->getGroupbys();

        $list = [];

        $class = new StatReport($params);
        $sql   = $class->getSqlByGroup();
        $stat  = ModelBase::getInstance()->select($sql);
        $data  = $stat->rows();
        foreach ($data as $val) {
            $index = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                $index .= $v;
                $tmp[$key] = $v;
            }
            $index = md5($index);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
            }

            foreach ($template as $key => $normal) {
                $list[$index][$key] += $val[$key] ?? 0;
            }
        }

        $total = $template;
        $sql   = $class->getSql();
        $stat  = ModelBase::getInstance()->select($sql);
        $data  = $stat->rows();
        foreach ($data as $val) {
            foreach ($template as $key => $normal) {
                $total[$key] += $val[$key] ?? 0;
            }
        }

        return [
            'list'     => $list,
            'total'    => $total,
            'allsum'   => $total,
            'groupbys' => $groupbys
        ];
    }

    private function getKeepStatDateByTime($groupbys, $time)
    {
        $date = '';
        foreach ($groupbys as $name) {
            switch ($name) {
                case 'stat_hour':
                    $date = date('H', $time);
                    break;
                case 'stat_date':
                    $date = date('Y-m-d', $time);
                    break;
                case 'stat_week':
                    $date = date('Y-W', $time);
                    break;
                case 'stat_month':
                    $date = date('Y-m', $time);
                    break;
                default:
                    break;
            }
            if ($date != '') {
                return [$name, $date];
            }
        }

        return ['stat_date', $time];
    }

    public function statKeep($params)
    {
        $keep_type = $params['keep_type'] ?? 0;
        if ($keep_type == 8) {
            $class = new StatRoleKeep($params);
        } else {
            $class = new StatKeep($params);
        }
        $sqls = $class->getSqlByGroup();
        if (!$sqls) {
            return;
        }

        $groupbys = $class->getGroupbys();
        $etime    = $class->getEtime();
        $days     = $class->getKeepDays();
        $template = [
            "keeps" => [],
        ];
        for ($i = 0; $i <= count($days); $i++) {
            $template['keeps'][$i] = [
                'reg'   => 0,
                'login' => 0,
            ];
        }
        $total = $template['keeps'];
        $today = strtotime('today');

        ModelBase::getInstance()->setTimeout(20);
        $list     = [];
        foreach ($sqls as $sql) {
            $stat = ModelBase::getInstance()->select($sql);
            // 分隔每一天数据，并发日期字段填补上
            $data = [];
            foreach ($stat->rows() as $val) {
                foreach ($val as $key => $keep_value) {
                    if (strpos($key, 'k') === 0 && is_array($keep_value)) {
                        $reg_time = substr($key, 1);
                        list($name, $date) = $this->getKeepStatDateByTime($groupbys, $reg_time);
                        $val[$name] = $date;

                        $tmp = [];
                        foreach ($groupbys as $gkey) {
                            $tmp[$gkey] = $val[$gkey];
                        }
                        $tmp[$key] = $keep_value;
                        $data[] = $tmp;
                    }
                }
            }

            foreach ($data as $val) {
                $index  = '';
                $tmp    = [];
                $future = false;
                foreach ($groupbys as $key) {
                    $v = $this->formatGroupValue($key, $val[$key]);
                    // 如果有进行日期分组，并且此天不在统计时间范围内
                    // if ($key == 'stat_date' && strtotime($v) >= $etime) {
                    //     $future = true;
                    //     break;
                    // }
                    $index .= $v;
                    $tmp[$key] = $v;
                }
                if ($future == true) {
                    continue;
                }
                $index = md5($index);
                if (!isset($list[$index])) {
                    $list[$index] = array_merge($tmp, $template);
                }

                foreach ($val as $key => $keep_value) {
                    if (strpos($key, 'k') === 0 && is_array($keep_value)) {
                        $reg_time = substr($key, 1);
                        if ($reg_time == $today) {
                            continue;
                        }
                        foreach ($keep_value as $id => $num) {
                            // 如果此天留存已超过今天，则此天注册不能用于今天计算
                            $keepday = $reg_time;
                            if ($id > 0) {
                                $keepday = $reg_time + 86400 * ($days[$id - 1] - 1);
                            }
                            if ($keepday <= $today) {
                                $list[$index]['keeps'][$id]['reg'] += $keep_value[0];
                                $total[$id]['reg'] += $keep_value[0];
                            }
                            $list[$index]['keeps'][$id]['login'] += $num;
                            $total[$id]['login'] += $num;
                        }
                    }
                }
            }
        }

        return [
            'days'     => $days,
            'list'     => $list,
            'total'    => $total,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }

    public function statLtv($params)
    {
        $class    = new StatRegLtv($params);
        $groupbys = $class->getGroupbys();
        $ltvDay   = $class->getLtv();

        $template = [
            'reg' => 0,
            "ltvs" => [],
        ];
        for ($i = 1; $i <= $ltvDay; $i++) {
            $template['ltvs'][$i] = [
                'pay_num'   => 0,
                'pay_money' => 0,
                'pay_money2' => 0,
            ];
        }

        $sql  = $class->getSqlByGroup();
        $stat = ModelBase::getInstance()->select($sql);
        $list = [];
        foreach ($stat->rows() as $val) {
            $index = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                $index .= $v;
                $tmp[$key] = $v;
            }
            $index = md5($index);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
            }

            $reg_day  = $val['cday'];
            $reg_time = strtotime($reg_day);
            foreach ($val['pay_days'] as $id => $pday) {
                $ltv = (strtotime($pday) - $reg_time) / 86400 + 1;
                if ($ltv <= $ltvDay) {
                    $list[$index]['ltvs'][$ltv]['pay_num'] += $val['pay_nums'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money'] += $val['pay_moneys'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money2'] += $val['pay_moneys2'][$id];
                }
            }
        }

        // 补齐数据和汇总数据
        $total = $template['ltvs'];
        foreach ($list as &$data) {
            for ($ltv = 1; $ltv <= $ltvDay; $ltv++) {
                $pay_num   = 0;
                $pay_money = 0;
                $pay_money2 = 0;
                if (isset($data['ltvs'][($ltv - 1)])) {
                    $pay_num   = $data['ltvs'][($ltv - 1)]['pay_num'];
                    $pay_money = $data['ltvs'][($ltv - 1)]['pay_money'];
                    $pay_money2 = $data['ltvs'][($ltv - 1)]['pay_money2'];
                }

                $data['ltvs'][$ltv]['pay_num'] += $pay_num;
                $data['ltvs'][$ltv]['pay_money'] += $pay_money;
                $data['ltvs'][$ltv]['pay_money2'] += $pay_money2;

                $total[$ltv]['pay_num'] += $data['ltvs'][$ltv]['pay_num'];
                $total[$ltv]['pay_money'] += $data['ltvs'][$ltv]['pay_money'];
                $total[$ltv]['pay_money2'] += $data['ltvs'][$ltv]['pay_money2'];
            }
        }

        return [
            'ltv'     => $ltvDay,
            'list'     => $list,
            'total'    => $total,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }

    public function statLtvMidas($params)
    {
        $class    = new StatRegLtvMidas($params);
        $groupbys = $class->getGroupbys();
        $ltvDay   = $class->getLtv();

        $template = [
            'reg' => 0,
            "ltvs" => [],
        ];
        for ($i = 1; $i <= $ltvDay; $i++) {
            $template['ltvs'][$i] = [
                'pay_num'   => 0,
                'pay_money' => 0,
                'pay_money2' => 0,
            ];
        }

        $sql  = $class->getSqlByGroup();
        $stat = ModelBase::getInstance()->select($sql);
        $list = [];
        foreach ($stat->rows() as $val) {
            $index = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                $index .= $v;
                $tmp[$key] = $v;
            }
            $index = md5($index);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
            }

            $reg_day  = $val['cday'];
            $reg_time = strtotime($reg_day);
            foreach ($val['pay_days'] as $id => $pday) {
                $ltv = (strtotime($pday) - $reg_time) / 86400 + 1;
                if ($ltv <= $ltvDay) {
                    $list[$index]['ltvs'][$ltv]['pay_num'] += $val['pay_nums'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money'] += $val['pay_moneys'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money2'] += $val['pay_moneys2'][$id];
                }
            }
        }

        // 补齐数据和汇总数据
        $total = $template['ltvs'];
        foreach ($list as &$data) {
            for ($ltv = 1; $ltv <= $ltvDay; $ltv++) {
                $pay_num   = 0;
                $pay_money = 0;
                $pay_money2 = 0;
                if (isset($data['ltvs'][($ltv - 1)])) {
                    $pay_num   = $data['ltvs'][($ltv - 1)]['pay_num'];
                    $pay_money = $data['ltvs'][($ltv - 1)]['pay_money'];
                    $pay_money2 = $data['ltvs'][($ltv - 1)]['pay_money2'];
                }

                $data['ltvs'][$ltv]['pay_num'] += $pay_num;
                $data['ltvs'][$ltv]['pay_money'] += $pay_money;
                $data['ltvs'][$ltv]['pay_money2'] += $pay_money2;

                $total[$ltv]['pay_num'] += $data['ltvs'][$ltv]['pay_num'];
                $total[$ltv]['pay_money'] += $data['ltvs'][$ltv]['pay_money'];
                $total[$ltv]['pay_money2'] += $data['ltvs'][$ltv]['pay_money2'];
            }
        }

        return [
            'ltv'     => $ltvDay,
            'list'     => $list,
            'total'    => $total,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }

    private function getShardByMoney($money, $shards)
    {
        for ($i=0; $i < count($shards); $i++) {
            if ($money <= $shards[$i]) {
                return $shards[$i];
            }
        }
    }

    public function statLtvFirstDayMoney($params, $shards = [0,6,30,68,128,198,328,648,1000000])
    {
        $class    = new StatRegLtvFirstDayMoney($params);
        $groupbys = $class->getGroupbys();
        $groupbys[] = 'cday_money';
        $ltvDay   = $class->getLtv();

        $template = [
            'reg' => 0,
            "ltvs" => [],
        ];
        for ($i = 1; $i <= $ltvDay; $i++) {
            $template['ltvs'][$i] = [
                'pay_user'   => 0,
                'pay_num'    => 0,
                'pay_money'  => 0,
                'pay_money2' => 0,
                'costindex'  => '',
            ];
        }

        $sql  = $class->getSqlByGroup();
        $stat = ModelBase::getInstance()->select($sql);
        $list = [];
        foreach ($stat->rows() as $val) {
            $index = '';
            $costindex = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                if ($key == 'cday_money') {
                    $v = $this->getShardByMoney($v, $shards);
                }
                $index .= $v;
                $tmp[$key] = $v;
                if ($key != 'cday_money') {
                    $costindex .= $v;
                }
            }
            $index = md5($index);
            $costindex = md5($costindex);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
                $list[$index]['costindex'] = $costindex;
            }

            $reg_day  = $val['cday'];
            $reg_time = strtotime($reg_day);
            foreach ($val['pay_days'] as $id => $pday) {
                $ltv = (strtotime($pday) - $reg_time) / 86400 + 1;
                if ($ltv <= $ltvDay) {
                    $list[$index]['ltvs'][$ltv]['pay_user'] += $val['pay_users'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_num'] += $val['pay_nums'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money'] += $val['pay_moneys'][$id];
                    $list[$index]['ltvs'][$ltv]['pay_money2'] += $val['pay_moneys2'][$id];
                }
            }
        }

        usort($list, function($a, $b) {
            return $a['cday_money'] > $b['cday_money'] ? 1 : -1;
        });

        // 补齐数据和汇总数据
        $totals = [];
        $totals2 = $template['ltvs'];
        foreach ($list as &$data) {
            $cday_money = $data['cday_money'];
            if (!isset($totals[$cday_money])) {
                $totals[$cday_money] = $template['ltvs'];
            }
            for ($ltv = 1; $ltv <= $ltvDay; $ltv++) {
                $pay_user   = 0;
                $pay_num    = 0;
                $pay_money  = 0;
                $pay_money2 = 0;
                if (isset($data['ltvs'][($ltv - 1)])) {
                    $pay_user   = $data['ltvs'][($ltv - 1)]['pay_user'];
                    $pay_num    = $data['ltvs'][($ltv - 1)]['pay_num'];
                    $pay_money  = $data['ltvs'][($ltv - 1)]['pay_money'];
                    $pay_money2 = $data['ltvs'][($ltv - 1)]['pay_money2'];
                }

                $data['ltvs'][$ltv]['pay_user'] += $pay_user;
                $data['ltvs'][$ltv]['pay_num'] += $pay_num;
                $data['ltvs'][$ltv]['pay_money'] += $pay_money;
                $data['ltvs'][$ltv]['pay_money2'] += $pay_money2;

                $totals[$cday_money][$ltv]['pay_user'] += $data['ltvs'][$ltv]['pay_user'];
                $totals[$cday_money][$ltv]['pay_num'] += $data['ltvs'][$ltv]['pay_num'];
                $totals[$cday_money][$ltv]['pay_money'] += $data['ltvs'][$ltv]['pay_money'];
                $totals[$cday_money][$ltv]['pay_money2'] += $data['ltvs'][$ltv]['pay_money2'];

                $totals2[$ltv]['pay_user'] += $data['ltvs'][$ltv]['pay_user'];
                $totals2[$ltv]['pay_num'] += $data['ltvs'][$ltv]['pay_num'];
                $totals2[$ltv]['pay_money'] += $data['ltvs'][$ltv]['pay_money'];
                $totals2[$ltv]['pay_money2'] += $data['ltvs'][$ltv]['pay_money2'];
            }
        }

        return [
            'ltv'      => $ltvDay,
            'list'     => $list,
            'total'    => $totals,
            'total2'   => $totals2,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }

    public function statRoi($params)
    {
        $class    = new StatRegRoi($params);
        $groupbys = $class->getGroupbys();

        $template = [
            'pay_roi_num'   => 0,
            'pay_roi_money' => 0,
            'pay_roi_money2' => 0,
        ];

        $sql  = $class->getSqlByGroup();
        $stat = ModelBase::getInstance()->select($sql);
        $list = [];
        foreach ($stat->rows() as $val) {
            $index = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                $index .= $v;
                $tmp[$key] = $v;
            }
            $index = md5($index);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
            }

            $list[$index]['pay_roi_num'] += $val['pay_num'];
            $list[$index]['pay_roi_money'] += $val['pay_money'];
            $list[$index]['pay_roi_money2'] += $val['pay_money2'];
        }

        // 补齐数据和汇总数据
        $total = $template;
        foreach ($list as $data) {
            $total['pay_roi_num'] += $data['pay_roi_num'];
            $total['pay_roi_money'] += $data['pay_roi_money'];
            $total['pay_roi_money2'] += $data['pay_roi_money2'];
        }

        return [
            'list'     => $list,
            'total'    => $total,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }

    public function statRoiMidas($params)
    {
        $class    = new StatRegRoiMidas($params);
        $groupbys = $class->getGroupbys();

        $template = [
            'midas_pay_roi_num'   => 0,
            'midas_pay_roi_money' => 0,
            'midas_pay_roi_money2' => 0,
        ];

        $sql  = $class->getSqlByGroup();
        $stat = ModelBase::getInstance()->select($sql);
        $list = [];
        foreach ($stat->rows() as $val) {
            $index = '';
            $tmp   = [];
            foreach ($groupbys as $key) {
                $v = $this->formatGroupValue($key, $val[$key]);
                $index .= $v;
                $tmp[$key] = $v;
            }
            $index = md5($index);

            if (!isset($list[$index])) {
                $list[$index] = array_merge($tmp, $template);
            }

            $list[$index]['midas_pay_roi_num'] += $val['pay_num'];
            $list[$index]['midas_pay_roi_money'] += $val['pay_money'];
            $list[$index]['midas_pay_roi_money2'] += $val['pay_money2'];
        }

        // 补齐数据和汇总数据
        $total = $template;
        foreach ($list as $data) {
            $total['midas_pay_roi_num'] += $data['midas_pay_roi_num'];
            $total['midas_pay_roi_money'] += $data['midas_pay_roi_money'];
            $total['midas_pay_roi_money2'] += $data['midas_pay_roi_money2'];
        }

        return [
            'list'     => $list,
            'total'    => $total,
            'groupbys' => $groupbys,
            'template' => $template,
        ];
    }
}
